package com.example.klinedemo

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.example.klinedemo.carame.CarameView

class MainActivity : AppCompatActivity() {

    private val viewModel:MainActivityViewModel by lazy {
        ViewModelProvider(this)[MainActivityViewModel::class.java]
    }

    fun checkPermission(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissions(
                arrayOf(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
                ), 1
            )
        }
        return false
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Kotlin 方式
        val displayMetrics = resources.displayMetrics
        val screenHeightPx = displayMetrics.heightPixels

        val viewGroup = LayoutInflater.from(this).inflate(R.layout.activity_main,null) as ViewGroup
        setContentView(viewGroup)
        checkPermission()
        val carameView = CarameView(this)


        // val kLineView = KLineView(this,lifecycle,lifecycleScope)
        viewGroup.addView(carameView,ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,screenHeightPx/2))
        viewModel.initData(this)
//        viewModel.liveData.observe(this){data ->
//            kLineView.updateValue(data.klineData)
//        }
//

    }


    override fun onResume() {
        super.onResume()

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || requestCode != 1) return
    }




}